package com.nengchuan.kbms.dfs.service;

import com.nengchuan.config.build.AuthUtils;
import com.nengchuan.config.exception.NcException;
import com.nengchuan.kbms.common.AutoWrapper;
import com.nengchuan.kbms.common.DfsTreeNode;
import com.nengchuan.kbms.common.PageServiceImpl;
import com.nengchuan.kbms.dfs.constant.BaseConstant;
import com.nengchuan.kbms.dfs.dao.UserFolderMapper;
import com.nengchuan.kbms.dfs.entity.dto.UserFolderDTO;
import com.nengchuan.kbms.dfs.entity.po.UserFolder;
import com.nengchuan.tree.util.NcTreeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 用户文件夹 服务实现类
 * </p>
 *
 * @author abe
 * @since 2020-03-30
 */
@Service
public class UserFolderServiceImpl extends PageServiceImpl<UserFolderMapper, UserFolder> implements UserFolderService {

    @Autowired
    private UserFolderMapper userFolderMapper;

    /**
     * 新增用户文件夹
     */
    @Override
    public Long saveFolder(UserFolderDTO dto) {
        // 用户父文件夹id没有传入的话默认为在该用户的根目录新增文件夹
        if (dto.getParentId() == null) {
            dto.setParentId(BaseConstant.Folder.ROOT_ID);
        }
        if (!BaseConstant.Folder.ROOT_ID.equals(dto.getParentId())) {
            assertFolderExisted(dto.getParentId());
        }
        dto.setFolderName(trimRight(dto.getFolderName()));
        dto.setUserId(AuthUtils.getUserId());
        UserFolder dataFolder = getOne(new AutoWrapper<>(dto), false);
        if (dataFolder != null) {
            throw new NcException("文件名已存在，无法创建: " + dto.getFolderName());
        }
        return insert(dto).getId();
    }

    /**
     * 更新用户文件夹
     */
    @Override
    public boolean updateFolder(UserFolderDTO dto) {
        dto.setFolderName(trimRight(dto.getFolderName()));
        UserFolder dataFolder = getOne(new AutoWrapper<>(dto), false);
        if (dataFolder != null && dataFolder.getId() != dto.getId().longValue()) {
            throw new NcException("文件名已存在，无法修改: " + dto.getFolderName());
        }
        return update(dto);
    }

    /**
     * 删除用户文件夹
     */
    @Override
    public boolean removeFolder(Serializable id) {
        return remove(id);
    }

    @Override
    public List<DfsTreeNode> tree(Long rootId) {
        if (rootId == null) {
            rootId = BaseConstant.Folder.ROOT_ID;
        }
        Long userId = AuthUtils.getUserId();
        List<DfsTreeNode> nodeList = userFolderMapper.userFolderTree(userId, rootId);
        nodeList.forEach(node -> node.setPart(BaseConstant.EQUIPMENT));
        return (List<DfsTreeNode>) NcTreeUtil.transform(nodeList);
    }

    /**
     * 断言用户文件夹存在
     */
    @Override
    public void assertFolderExisted(Long folderId) {
        UserFolder userFo = getById(folderId);
        if (userFo == null) {
            throw new NcException("文件夹不存在: " + folderId);
        }
    }

    /**
     * 移除字符串右边空格
     */
    private String trimRight(String value) {
        if (value != null) {
            value = value.replaceAll("\\s+$", "");
        }
        return value;
    }
}
